<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">版本检测</block>
		</cu-custom>
		<view class="margin-top-xl text-center solid padding bg-white">
			<view>当前版本: {{version}}</view>
			<view class="cu-btn margin-top-xl padding" @tap="checkApkVersion">立即更新</view>
		</view>
	</view>
</template>

<script>
	var _self
	export default {
		data() {
			return {
				version: null
			}
		},
		onLoad() {
			_self = this
			this.version = plus.runtime.version
		},
		methods: {
			checkApkVersion() {
				console.log("check apk version " + plus.runtime.version)
				// 锁定屏幕方向
				plus.screen.lockOrientation('portrait-primary'); //锁定
				// 检测升级
				console.log(_self.api + '/update/check');
				uni.request({
					url: _self.api + '/update/check', //检查更新的服务器地址
					method: 'POST',
					data: {
						appid: plus.runtime.appid,
						version: plus.runtime.version,
						imei: plus.device.imei
					},
					success: (res) => {
						if (res.statusCode == 200) {
							if (res.data.data.isUpdate) {
								console.log("check apk version reps ")
								console.log(res.data.data)
								let openUrl = plus.os.name === 'iOS' ? res.data.data.iosUrl : res.data.data.androidUrl;
								_self.setChannel(res.data.data.channelName)
								// 提醒用户更新L
								uni.showModal({
									title: '发现新版本',
									content: res.data.data.desc,
									success: (showResult) => {
										if (showResult.confirm) {
											plus.runtime.openURL(openUrl);
										}
									}
								})
							}else{
								uni.showToast({
									title:'已是最新版本'
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>
